/**
 * parm:给定一个大小
 * Parm:给这个值一个初始值，默认是0
 */
//分配10个字节大小的，值为0
var buf = Buffer.alloc(10,0xff);
console.log(buf);

// (1) 给定分配一个给定大小的Buffer的内存
// (2) 不会对这些内存区赋初值的,随机的数据，它原来是什么就是什么；
// Unsafe 指的是没有初始化的内存
buf = Buffer.allocUnsafe(10);
console.log(buf);

// 不重Buffer缓冲区里面分配，直接从操作系统分配
// Slow指的是没有重缓冲池里面高效的分配
// Unsafe, 指的是内存，没有被初始化
buf = Buffer.allocUnsafeSlow(10);  
console.log(buf); 

console.log(buf.length);

// 方便的创建方式，复制
// 创建一个Buffer对象，用来存放这个字符串的二进制
buf = Buffer.from("HelloWorldHelloWorld");
console.log(buf.length);

buf = Buffer.from([1,2,3,4,5,6,7,8]);
console.log(buf.length);

console.log("===================")
// Hellow -> 48 65 6c 6c 6f 57 6f 72 6c 64
var b = Buffer.alloc(8);
console.log(b);
b.write("HelloWorld",0)
console.log(b);
b.write("HelloWorld",1)
console.log(b);
b.write("HelloWorld",2)
console.log(b);


var b1 = Buffer.alloc(16);
console.log(b1);
// 以大尾的形式存放 4个字节的整数
// 0x00 00 ff ff --->655535
// 00 00 ff ff
// start从哪里开始
b1.writeInt32BE(17,0)
console.log(b1,b1.length);
// b1.writeInt32BE(65535,1)
// console.log(b1);
// b1.writeInt32BE(65535,2)
// console.log(b1);

console.log( b1.readInt32BE(0), b1.readInt32BE(1), b1.readInt32BE(2), b1.readInt32BE(3));
console.log( b1.readInt32LE(0), b1.readInt32LE(1), b1.readInt32LE(2), b1.readInt32LE(3));

var b2 = Buffer.alloc(16);
console.log(b2);
b2.writeInt32LE(17, 0);
b2.writeInt32LE(17, 1);
b2.writeInt32LE(17, 2);
b2.writeInt32LE(17, 3);
console.log(b2);

var bf = Buffer.alloc(4 * 4);
bf.writeInt32LE(65535, 0);
bf.writeInt32LE(65535, 4);
bf.writeInt32LE(65535, 8);
bf.writeInt32LE(65535, 12);
console.log(bf);

var bu = Buffer.from([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
console.log(bu);
bu.swap16();
// 3, 2, 1, 0,| 7, 6, 5, 4, |11, 10, 9, 8, |15, 14, 13, 12
console.log(bu);

console.log(bf.toString('utf8'));
console.log(bf.toString('hex'));
console.log(bu.toJSON());

buf.fill('A');
console.log(buf);
console.log("utf8",buf.toString('utf8'));

buf.fill("Hello",0,5);
console.log(buf);
console.log("utf8",buf.toString('utf8'));